package com.wifi.eshipewifi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.wifi.BaseApplication;
import com.wifi.imp.MessageSend;

public class InitActivity extends Activity implements View.OnClickListener,MessageSend {

    public String TAG = "InitActivity";

    //引导图片资源
    private static final int pics = R.drawable.start;


    private Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
                try {
                    if (msg.obj != null) {
                        String s = msg.obj.toString();
                        if (s.trim().length() > 0) {
                            Log.i(TAG, "mhandler接收到obj=" + s);
                            String s1[] = s.split(";");
                            if (s1[0].indexOf("init") != -1) {//首页数据
                                if (s1.length > 2) {
                                    application.makeToast(InitActivity.this,s1[1],true);
                                }
                            } else if (s1[0].indexOf("first_connect") != -1) {
                                application.makeToast(InitActivity.this,"连接成功",true);
                            }
                        }
                    }

                    Log.i(TAG, "mhandler接收到msg" + msg.obj.toString());
                } catch (Exception ee) {
                    Log.i(TAG, "加载过程出现异常");
                    ee.printStackTrace();
                }
            }
    };
    private Handler mhandlerSend = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

            }
        }
    };

    SharedPreferences sp;
    SocThread thread;
    BaseApplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);
//        ImageView iv_start = (ImageView) findViewById(R.id.iv_start);
//        Bitmap b = ImageCache.getBitmap(pics);
//        iv_start.setImageBitmap(b);
//        iv_start.setScaleType(ImageView.ScaleType.FIT_XY);//拉伸
        application = (BaseApplication) getApplication();
//        String ip = application.getIp();
//        thread = new SocThread(mhandler, mhandlerSend, InitActivity.this, ip);
//        thread.start();
//        application.setThread(thread);
        postDelayed();

//todo 暂时不需要        sp = this.getSharedPreferences("SP", MODE_PRIVATE);
//        SharedPreferences.Editor editor = sp.edit();
//        if(TextUtils.isEmpty(sp.getString("ipstr", ""))){//设置默认地址和端口
//            editor.putString("ipstr", "192.168.8.1");
//            editor.putString("port", "8888");
//            editor.commit();//保存新数据
//        }else if(sp.getString("ipstr", "").indexOf("192.168.43.151")!=-1){//todo 有问题
//            editor.putString("ipstr", "192.168.4.1");
//            editor.putString("port", "8000");
//            editor.commit();//保存新数据
//        }
    }

    //内存销毁
    @Override
    protected void onDestroy() {
        super.onDestroy();
//        try {
//            ImageCache.clearBitmap(R.drawable.start);
//        } catch (Exception e) {
//        }
    }


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Log.d("StartActivity", "初始成功，进入主界面");
            start();
        }
    };

    private void start() {
        Intent i = new Intent(this, LoginActivity.class);
        startActivity(i);
        this.finish();
    }

    //延时进入主界面
    public void postDelayed() {
        mhandler.postDelayed(runnable, 3000);//延时3miao进入主界面
    }


    @Override
    public void SendMessage(String msg) {
        if(msg!=null)
            thread.Send(msg);
    }

    @Override
    public void GetNewThread(SocThread thread) {
        //todo
    }

    @Override
    public void onClick(View v) {

    }
}
